package cn.kgc.mvc.day02.controller;

import cn.kgc.mvc.day02.entity.Teacher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 课工场
 * @date 2024/7/19
 * @description  RequestMapping的衍生注解
 * 后续可以根据方法的实现逻辑使用不同的注解进行修饰    restful
 * 1.get    获取数据    /teacher/list
 * 2.post    添加数据
 * 3.put      修改
 * 4.delete   删除
 *
 *
 * springmvc框架中如何实现参数的接收
 *  1. 零散参数的接收
 *  2. 对象类型的接收
 *  3. json格式数据   轻量级的数据传输格式 json字符串  "{name:tom,age:20,salary:1500}"  前后端分离  json
 *  4. 数组类型
 *  5. 集合类型
 *
 *  接收参数的形式
 *    1. HttpServletRequest request
 *    2. 声明形参的形式
 */
@Controller
@RequestMapping("teacher")
public class TeacherController {


    // 教师信息列表
    @GetMapping("list")
    public ModelAndView  teacherList(){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----------teacherList------------");
        return modelAndView;
    }


    @PostMapping("add1")
    public ModelAndView  addTeacher(HttpServletRequest request){

        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String salary = request.getParameter("salary");

        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----------addTeacher------------");
        return modelAndView;
    }
    /**
     * 添加
     *  1. 多个零散参数接收可以直接在参数列表中进行声明  但是需要满足形参的名字和数据传输的key一致  和input表单中的name属性值一致
     *       如果参数和key不一致   可以通过@RequestParam("ag")来指定对应的key  但通过注解指定后 则参数必传 ag  因为注解中有一个默认的
     *       注解属性 required() default true; 可以通过设置false覆盖默认值
     *  2. 接收零散参数时数据是基本类型数据 数据的类型尽可能的使用包装类型, 如果使用基本类型数据接收则参数必传
     *      IllegalStateException: Optional int parameter 'age' is present but cannot be translated into a null value
     *      int  age = null
     *
     */
    @PostMapping("add2")
    public ModelAndView  addTeacher2(String name,@RequestParam(value = "ag",required = false) Integer age,Double salary){

        Teacher teacher = new Teacher();
        teacher.setAge(age);
        teacher.setName(name);
        teacher.setSalary(salary);

        // TODO  调用业务层

        System.out.println("name = " + name + ", age = " + age + ", salary = " + salary);

        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----------addTeacher------------");
        return modelAndView;
    }


    //  使用对象类型实现参数的接收  request中携带参数的key要和对象中的属性名保持一致 ?name=tom&age=20
    @PostMapping("add3")
    public ModelAndView  addTeacher3(Teacher teacher){

        System.out.println("teacher = " + teacher);

        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----------addTeacher------------");
        return modelAndView;
    }

    //  Json格式数据接收    "{name:tom,age:20,salary:1500}"   json => java object        fastJson  hutool  jackson
    // @RequestBody   标识当前参数是json格式数据 springmvc借助jackson工具实现数据类型转换
    @PostMapping("add4")
    public ModelAndView  addTeacher4(@RequestBody Teacher teacher){

        System.out.println("teacher = " + teacher);

        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----------addTeacher------------");
        return modelAndView;
    }

    @PostMapping("add5")
    public ModelAndView  addTeacher5(@RequestBody  Map<String,Object> params){

        System.out.println("params = " + params);

        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----------addTeacher------------");
        return modelAndView;
    }



    // 修改
    @PutMapping("edit")
    public ModelAndView  editTeacher(){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----------editTeacher------------");
        return modelAndView;
    }

    // 删除   /teacher/delete?id=1
    @DeleteMapping("delete")
    public ModelAndView  deleteTeacher(Integer id){
        System.out.println("id = " + id);

        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----------deleteTeacher------------");
        return modelAndView;
    }


    //  /teacher/del/1       ?id=1
    @DeleteMapping("del/{id}")
    public ModelAndView  deleteTeacher2(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----------deleteTeacher------------");
        return modelAndView;
    }

    // 删除  以数组的形式接收参数
    @DeleteMapping("batchDelete")
    public ModelAndView  batchDeleteTeacher(Integer[] ids){

        for (Integer id : ids) {
            System.out.println("id = " + id);
        }

        ModelAndView modelAndView = new ModelAndView();
        System.out.println("-----------deleteTeacher------------");
        return modelAndView;
    }

}
